import {CallHandler,ExecutionContext,Injectable,NestInterceptor} from '@nestjs/common';
import {Observable} from 'rxjs';
import { map } from 'rxjs/operators';
  

  //全局返回体拦截器(RESTFUL API 风格)   
  @Injectable()
  export class GlobalInterceptors implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map((result) => {
        if (result && result.__code) {
          return {
            code: result.__code,
            message: result.__message,
            data: null,
          };
        }
        return {
          code: 200,
          message: '请求成功!',
          data: result,
        };
      }),
    );
  }
}

